package lerry.interaction;

import flash.display.InteractiveObject;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TouchEvent;
import flash.ui.Mouse;
import lerry.display.StageInfo;

/**
 * ...
 * @author ...
 */
class Interaction{
	private static var mouseDown:MouseEvent -> Void;
	private static var mouseUp:MouseEvent -> Void;
	private static var mouseOver:MouseEvent -> Void;
	private static var mouseOut:MouseEvent -> Void;
	private static var click:MouseEvent -> Void;
	private static var mouseMove:MouseEvent ->Void;
	
	private static var touchBegin:TouchEvent -> Void;
	private static var touchEnd:TouchEvent -> Void;
	private static var touchMove:TouchEvent ->Void;
	/**
	 * 触摸开始
	 */
	static public function TouchBegin(source:InteractiveObject,listener:TouchEvent ->Void):Void {
		if (listener != null) {
			touchBegin = listener;
			source.addEventListener(TouchEvent.TOUCH_BEGIN, touchBegin);
		}else {
			if (touchBegin != null) {
				source.removeEventListener(TouchEvent.TOUCH_BEGIN, touchBegin);
				touchBegin = null;
			}
		}
	}
	/**
	 * 触摸结束
	 */
	static public function TouchEnd(source:InteractiveObject,listener:TouchEvent ->Void):Void {
		if (listener != null) {
			touchEnd = listener;
			source.addEventListener(TouchEvent.TOUCH_END, touchEnd);
		}else {
			if (touchEnd != null) {
				source.removeEventListener(TouchEvent.TOUCH_END, touchEnd);
				touchEnd = null;
			}
		}
	}
	/**
	 * 触摸移动
	 */
	static public function TouchMove(source:InteractiveObject,listener:TouchEvent ->Void):Void {
		if (listener != null) {
			touchMove = listener;
			source.addEventListener(TouchEvent.TOUCH_MOVE, touchMove);
		}else {
			if (touchMove != null) {
				source.removeEventListener(TouchEvent.TOUCH_MOVE, touchMove);
				touchMove = null;
			}
		}
	}

	/**
	 * 鼠标按下
	 */
	static public function MouseDown(source:InteractiveObject, listener:MouseEvent -> Void):Void {
		if (listener != null) {
			mouseDown = listener;
			source.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
		}else {
			if (mouseDown != null) {
				source.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
				mouseDown = null;
			}
		}
	}
	/**
	 * 鼠标经过
	 */
	static public function MouseOver(source:InteractiveObject, listener:MouseEvent -> Void):Void {
		if (listener != null) {
			mouseOver = listener;
			source.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
		}else {
			if (mouseOver != null) {
				source.removeEventListener(MouseEvent.MOUSE_OVER, mouseOver);
				mouseOver = null;
			}
		}
	}
	/**
	 * 鼠标移出
	 */
	static public function MouseOut(source:InteractiveObject, listener:MouseEvent -> Void):Void {
		if (listener != null) {
			mouseOut = listener;
			source.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
		}else {
			if (mouseOut != null) {
				source.removeEventListener(MouseEvent.MOUSE_OUT, mouseOut);
				mouseOut = null;
			}
		}
	}
	/**
	 * 鼠标抬起
	 */
	static public function MouseUp(source:InteractiveObject, listener:MouseEvent -> Void):Void {
		if (listener != null) {
			mouseUp = listener;
			source.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}else {
			if (mouseUp != null) {
				source.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
				mouseUp = null;
			}
		}
	}
	/**
	 * 鼠标点击
	 */
	static public function MouseClick(source:InteractiveObject, listener:MouseEvent -> Void):Void {
		if (listener != null) {
			click = listener;
			source.addEventListener(MouseEvent.CLICK, click);
		}else {
			if (click != null) {
				source.removeEventListener(MouseEvent.CLICK, click);
				click = null;
			}
		}
	}
	/**
	 * 鼠标移动
	 */
	static public function MouseMove(source:InteractiveObject, listener:MouseEvent -> Void):Void {
		if (listener != null) {
			mouseMove = listener;
			source.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
		}else {
			if (mouseMove != null) {
				source.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
				mouseMove = null;
			}
		}
	}
	static var keys:Array<UInt>;
	static public function keyDown(code:UInt):Bool {
		if (keys == null) {
			keys = new Array<UInt>();
		}
		if (!StageInfo.STAGE.hasEventListener(KeyboardEvent.KEY_DOWN)) {
			StageInfo.STAGE.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		if (!StageInfo.STAGE.hasEventListener(KeyboardEvent.KEY_UP)) {
			StageInfo.STAGE.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}	
		//if (keys.exists(code)) {
			//return keys.get(code);
		//}
		return Lambda.has(keys, code);
	}
	static public function keyUp(code:UInt):Bool {
		if (keys == null) {
			keys = new Array<UInt>();
		}
		if (!StageInfo.STAGE.hasEventListener(KeyboardEvent.KEY_DOWN)) {
			StageInfo.STAGE.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		if (!StageInfo.STAGE.hasEventListener(KeyboardEvent.KEY_UP)) {
			StageInfo.STAGE.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}	
		//if (keys.exists(code)) {
			//return keys.get(code);
		//}
		return !Lambda.has(keys, code);
	}
	static private function onKeyUp(e:KeyboardEvent):Void {
		if (Lambda.has(keys, e.keyCode)) {
			keys.remove(e.keyCode);
		}
	}
	
	static private function onKeyDown(e:KeyboardEvent):Void {
		if (!Lambda.has(keys, e.keyCode)) {
			keys.push(e.keyCode);
		}
		//keys.set(e.keyCode, true);
	}
	static var index:UInt = 0;
	static public function keyHit(code:UInt):Void {
		
	}
}